;(function(root) {
    // window.baseUrl = "http://www.hntxrj.com:9099/spterp/"; //路径
    // window.baseUrl = "http://192.168.0.123:8080/spterp/"; //路径
    // window.baseUrl = "http://114.115.208.88:8080/spterp/"; //路径
    window.baseUrl = "http://erp.hntxrj.com/"; //路径

    // window.baseUrl = "http://localhost:8080/spterp/"
	//http://www.hntxrj.com:9099/spterp/
    var timeoutTime = 40000; //超时 40秒

	root.CODEMESSAGE = {
        ERRORCOD : 0, //后台返回数据失败
		SUCCESSCODE : 0 //成功的code值
	}


	function extender(p,c) {
		for(var i in c){
			c[i] = p[i]
		}
    }
	// 新版成功状态码判断
	root.STATES = {
		SUCCESS : 0, //成功状态 为0
		isSuccess : function (data,success,error) {
			if(data.code == this.SUCCESS){
				if(typeof data.data === "object"){
                    // 数据正常 状态码正常的 成功
					success(data);
				}else{
					//状态码正常 数据不正常的 失败
					error(data);
				}

			}else{
                //状态码正常的 失败
                error(data);
			}
        },
		ioError : function (root,data) {
			root.console.log(data && data.msg || "获取数据失败");
        },
		netError : function () {
			return "获取数据失败";
        },
		outCode : function (msg) {
			var msger = "";
			switch (msg){
				case "重复":
                    msger = "添加数据重复";
                    break;
                case "成功":
                    msger = "操作成功";
                    break;
                case "失败":
                    msger = "操作失败";
                    break;
			}


			return msger ? msger : msg;
        }
	}


	function extender(p,c){
		for(var i in c){
			p[i] = c[i];
		}

		return p;
	}

	// 通用get ajax 方式
	function sendAjax(url,json,success,error,copyer){

        json.compid = rose.util.getItem('compid');
        json.opid = rose.util.getItem('opid');
        json.token = rose.util.getItem('token');
        json.empname = rose.util.getItem('empname');

        json.admin_compid = rose.util.getItem('admin_compid');
        json.admin_opid = rose.util.getItem('admin_opid');
        json.admin_token = rose.util.getItem('admin_token');
        json.admin_empname = rose.util.getItem('admin_empname');



		 $.ajax(extender({
            url : baseUrl+url,
            data : json,
			 headers:{"app":"android"},
            success : function(data, textStatus, request){
                if (request.getAllResponseHeaders().indexOf('REDIRECT') != -1){
                    //这里的重定向后期要用到
                    document.location.href = window.baseUrl+"login1.html";
                    rose.util.setItem('jumpType','REDIRECT');
                    console.log('jump');
                }
                success(data);
            },
            timeout : timeoutTime,
            async: true,
            error : function(xhr,key){
                error && error();
                if(key == "timeout"){
                    try{
                        layer.closeAll();
                        layer.open({
                            content: "请求数据超时",
                            skin: 'msg',
                            time: 4
                        });
                    }catch(e){}
                }
            }
        },copyer || {}))

	}

	// 通用post 方式
	function postAjax(url,json,success,error,copyer){
        json.compid = rose.util.getItem('compid');
        json.opid = rose.util.getItem('opid');
        json.token = rose.util.getItem('token');
        json.empname = rose.util.getItem('empname');

        json.admin_compid = rose.util.getItem('admin_compid');
        json.admin_opid = rose.util.getItem('admin_opid');
        json.admin_token = rose.util.getItem('admin_token');
        json.admin_empname = rose.util.getItem('admin_empname');


        $.ajax(extender({
			url : baseUrl+url,
			type : "post",
			data : json,
			success : function(data, textStatus, request){
                if (request.getAllResponseHeaders().indexOf('REDIRECT') != -1){

                	//这里的重定向后期要用到
                    document.location.href = window.baseUrl+"login1.html";
                    rose.util.setItem('jumpType','REDIRECT')
                    console.log('jump')
                }
                success(data);
			},
            beforeSend: function () {
               // console.log(arguments,'<--------')
            },
            timeout : timeoutTime,
            async: true,
			error : function(xhr,key){
                error && error();
                if(key == "timeout"){
                	try{
                		layer.closeAll();
                        layer.open({
                            content: "请求数据超时",
                            skin: 'msg',
                            time: 4
                        });
					}catch(e){}
				}
			}
		},copyer || {}))


	}

	// 暴露在全局的 api
	// 项目地址详见 http://apizza.cc/

	root.API = {
		logOut : function (fn) { // 退出登录
            if(window.localStorage){
                localStorage.removeItem('compid');
                localStorage.removeItem('opid');
                localStorage.removeItem('token');
                localStorage.removeItem('empname');
			}else{
                var config = {
                    path: '/',
                    expires : -10000000 //七天
                }

                $.fn.cookie('opid','',config);
                $.fn.cookie('token','',config);
                $.fn.cookie('empname','',config);
                $.fn.cookie('api','',config);
			}


            fn && fn();
        },
		spQuerySimpleContract : function(json,success,error){ // 合同列表
			postAjax('contract/spQuerySimpleContract',json,success,error)
		},
		spQuerySimpleContractRem : function(json,success,error){ // 查询合同详情
			sendAjax('contract/spQuerySimpleContractRem',json,success,error)
		},
		getTaskList : function(json,success,error){ // 加载任务单列
			postAjax('produce/getTaskList',json,success,error)
		},
		login : function(json,success,error){ // 登陆
            postAjax('login',json,success,error)
		},
		spinsertSMSimpleContractSave : function(json,success,error){ // 添加合同
		 	postAjax('contract/spinsertSMSimpleContractSave',json,success,error)
		},
		spQueryDDPublicInfo : function(json,success,error){ // 合同类型下拉列表
			sendAjax('contract/spQueryDDPublicInfo.do',json,success,error)
		},
		spQuerySMBusinessGroup : function(json,success,error){ // 业务员下拉
			postAjax('contract/spQuerySMBusinessGroup.do',json,success,error)
		},
		spQueryPriceDDPublicInfo : function(json,success,error){ // 合同价格执行方式下拉
		 	sendAjax('contract/spQueryPriceDDPublicInfo.do',json,success,error)
		},
		getEppList : function(json,success,error){ // 获取工程名称下拉
			postAjax('produce/getEppList',json,success,error)
		},
		getBuilderList : function(json,success,error){ // 获取施工单位列下拉
			postAjax('produce/getBuilderList',json,success,error)
		},
		fullDown : function(type,json,success,error){ // int类型(1 浇筑方式 2 水泥品种 3 石料要求 4 泵送否 5 塌落度 6 任务单类型 7 企业信息下拉 8 任务状态 9 小票类型)
			sendAjax('fullDown/type/'+type+'.do',json,success,error)
		},
		getStates : function(json,success,error){ //获取筛选状态
			sendAjax('fullDown/type/8.do',json,success,error);
		},
		getStates2 : function(json,success,error){ //获取筛选状态
			sendAjax('fullDown/type/11.do',json,success,error);
		},
		spinsertUpDelSMEPPInfo : function(json,success,error){ // 添加修改删除工程名称
			postAjax('contract/spinsertUpDelSMEPPInfo',json,success,error)
		},
		insertUpDelBuilder : function(json,success,error){ // 增/删/改  施工单位
			postAjax('produce/insertUpDelBuilder',json,success,error)
		},
		loadTaskDetail : function(json,success,error){// 加载任务单详情
			sendAjax('produce/loadTaskDetail',json,success,error)
		},
		getEppPlacing : function(json,success,error){// 加载浇灌部位
			sendAjax('produce/getEppPlacing',json,success,error)
		},
		getStgid : function(json,success,error){ // 获取砼标号
			postAjax('fullDown/getStgid',json,success,error)
		},
		addTaskPlan : function(json,success,error){ //添加任务单
			postAjax('produce/addTaskPlan',json,success,error)
		},
		spinsertUpDelSMC : function(json,success,error){ //合同砼标号添加存储过程
			postAjax('contract/spinsertUpDelSMContractGradePriceDetail',json,success,error);
		},
		spQuerySMStgidInfoPrice : function(json,success,error){ //1查询砼价格     2特殊材料  3泵车类价格   全查
		 	postAjax('contract/spQuerySMStgidInfoPrice',json,success,error)
		},
		getFullDown : function(json,success,error){ //fullDown/getFullDown下拉加载( 车号.司机.站别.搅拌楼.调度员 )带分页/可模糊查询
			postAjax('fullDown/getFullDown.do',json,success,error)
		},
		getTotalVehicle : function(json,success,error){//搅拌车工作量统计（砼运输。司机运输）
			postAjax('produce/getTotalVehicle',json,success,error)
		},
		spQuerySMContractGradePriceDetail : function(json,success,error){// 合同砼标号 查询语句
			postAjax('contract/spQuerySMContractGradePriceDetail',json,success,error)
		},
		spQuerySMContractPriceMarkup : function(json,success,error){ //合同特殊材料查询语句
			postAjax('contract/spQuerySMContractPriceMarkup',json,success,error)
		},
		spQuerySMPumpPriceSet : function(json,success,error){ //泵车类型
			postAjax('contract/spQuerySMPumpPriceSet',json,success,error)
		},
		spQuerySMContractDistanceSet : function(json,success,error){ //合同运距查询语句
			postAjax('contract/spQuerySMContractDistanceSet',json,success,error)
		},
		getTotalVehicle : function(json,success,error){ //搅拌车工作量统计（砼运输。司机运输）
			postAjax('produce/getTotalVehicle',json,success,error)
		},
		getTotalYield :function(json,success,error){ //砼产量统计
			postAjax('produce/getTotalYield',json,success,error)
		},
		spQueryVAccountPTProduceConsume : function(json,success,error){ //生产消耗汇总
			postAjax('consume/spQueryVAccountPTProduceConsume',json,success,error)
		},
		spQueryVMWorkClassLog : function(json,success,error){ //司机排班列表信息
			postAjax('consume/spQueryVMWorkClassLog',json,success,error)
		},
		spQueryLISTJson : function(json,success,error){ //保存砼 , 材料,  泵车价格    3.0
			postAjax('contract/spQueryLISTJson',json,success,error)
		},
		spinsertUpDelSMContractDistanceSet : function(json,success,error){ //合同运距添加
			postAjax('contract/spinsertUpDelSMContractDistanceSet',json,success,error)
		},
		spQueryVPTPlanFind : function(json,success,error){ //开具配比
			postAjax('laboratroy/spQueryVPTPlanFind',json,success,error)
		},
		getTaskListByEppAndBuild : function(json,success,error){ //任务单(根据工程名或施工单位名筛选)
			postAjax('produce/getTaskListByEppAndBuild',json,success,error)
		},
		getRealStock : function(json,success,error){ //实时库存示意图
			postAjax('produce/getRealStock',json,success,error)
		},
		spVerifyTask : function(json,success,error) {
			postAjax('produce/spVerifyTask',json,success,error)
		},
		spQueryUsercomp : function(json,success,error) { //获取企业
			postAjax('consume/spQueryUsercomp',json,success,error)
		},
	    spVerifyLMTaskTheoryFormula : function(json,success,error) { //开具生产配比审核
			postAjax('laboratroy/spVerifyLMTaskTheoryFormula',json,success,error)
		},
	    info : function(json,success,error) { //统计(二级菜单头部)
			sendAjax('total/info',json,success,error);
		},
		spQueryDDStirInfoSet : function(json,success,error){ //搅拌楼 即线号
			postAjax('contract/spQueryDDStirInfoSet.do',json,success,error)
		},
		adminEdit : function(json,success,error){ //企业  增-删-更
			postAjax('admin/editComp.action',json,success,error)
		},
		adminLoadComp : function(json,success,error) { // 加载企业-含分页
			postAjax('admin/loadComp.action',json,success,error)
		},
		adminEditComp : function(json,success,error) { // 企业  增-删-更
			postAjax('admin/editComp.action',json,success,error)
		},
		loadMenuInfo : function (json,success,error) { // 加载菜单
			postAjax('loadMenuInfo',json,success,error)
        },
        editEmployee : function (json,success,error) { // 用户 增-删-改
			postAjax('admin/editEmployee.action',json,success,error)
        },
        loadPermi : function (json,success,error) { // 加载权限组/用户组-用户组详情-含分页
        	postAjax('admin/loadPermi.action',json,success,error)
    	},
		adminSearchEmployee : function (json,success,error) {
			postAjax('admin/searchEmployee.action',json,success,error)
		},
        adminLoadMenu : function (json,success,error) {
			postAjax('admin/loadMenu.action',json,success,error)
        },
		adminLoadPermiSelected : function (json,success,error) { //获取已有权限ID
			postAjax('admin/loadPermiSelected.action',json,success,error)
    	},
        adminSavePermiss : function (json,success,error) {//admin 状态存
			postAjax('admin/savePermiss.action',json,success,error,{
                'processData': false,
                'contentType': 'application/json'
			})
    	},
        spVMPStockInQuerylistsrq : function (json,success,error) {//原材料过磅查询详情   及列表
			postAjax('stockin/spVMPStockInQuerylistsrq',json,success,error)
    	},
        deliveryTicketTotal:function (json,success,error) { //小票签收查询
			postAjax('produce/deliveryTicketTotal',json,success,error)
    	},
        spVMPStockInQueryManNsrq:function (json,success,error) { //原材料统计汇总
			postAjax('stockin/spVMPStockInQueryManNsrq',json,success,error)
    	},
        spQueryVMVehicleWeightListsrq:function (json,success,error) { // 搅拌车过磅查询
			postAjax('stockin/spQueryVMVehicleWeightListsrq',json,success,error)
    	},
        spVMPStockInQuerysql:function (json,success,error) { // 材料过磅统计
			postAjax('stockin/spVMPStockInQuerysql',json,success,error)
    	},
        builderSalaTotal : function (json,success,error) { // 销售方量统计
			postAjax('produce/builderSalaTotal',json,success,error)
        },
        vehicleCarTotal: function (json,success,error) {
			postAjax('produce/vehicleCarTotal',json,success,error)
    	},
        spVVMPumpTruckPersonalWorksrq: function (json,success,error) { // 泵车工作量统计
			postAjax('taskSale/spVVMPumpTruckPersonalWorksrq',json,success,error)
        },
        superAdmin:function (json,success,error) { // 管理员登录
			postAjax('admin/superAdmin.do',json,success,error)
    	},
        editPwd:function (json,success,error) { // 修改密码
			postAjax('editPwd',json,success,error)
        },
        parseData:function (json,success,error) { // 解析剪辑版
			postAjax('produce/parse.do',json,success,error)
    	},
        searchEmployee:function (json,success,error) {//加载用户列表
			postAjax('admin/searchEmployee.action',json,success,error)
    	},
        spupdataContractIsEffective: function (json,success,error) { //合同审核
			postAjax('contract/spupdataContractIsEffective',json,success,error)
        },
        editPermiGroup: function (json,success,error) { // 权限组 增-删-改
			postAjax('admin/editPermiGroup.action',json,success,error)
        },
        productTotal: function (json,success,error) { // 生产查询统计
			postAjax('produce/productTotal',json,success,error)
    	},
        vehicleCarTotal_Two: function (json,success,error) { // 调度派车(二次更改)
			postAjax('produce/vehicleCarTotal_Two',json,success,error)
    	},
        spquerydownsrq: function (json,success,error) { // 材料过磅  下拉
			postAjax('stockin/spquerydownsrq.do',extender(json,{
				id: 8
			}),success,error)
        },
		getXiaoPiao: function (json,success,error) { // 小票类型
            postAjax('fullDown/type/9.do',json,success,error)
        },
        getFullDownLouHao : function(json,success,error){ // 获取楼号
            postAjax('fullDown/getFullDown.do',extender(json,{
                type: 4
            }),success,error)
        },
		getJiaoZhu: function (json,success,error) { // 获取浇筑方式
            postAjax('fullDown/type/1.do',json,success,error)
        },
        spinsertfileImage: function (json,success,error) { // 小票签字图片上传
			postAjax('fileup/spinsertfileImage.do',json,success,error)
    	},
        spfiledown: function (json,success,error) { // 小票签字图片查看
			postAjax('fileup/spfiledown.do',json,success,error)
    	},
		getWxConfig: function (json,success,error) {
        	// url parmes
			postAjax('weixin/tick.do',json,success,error) // 获取微信配置
    	},
        spinsertLMTaskTheoryFormula: function (json,success,error) { // 配比修改
			postAjax('laboratroy/spinsertLMTaskTheoryFormula',json,success,error)
    	},
        stgidSavestgid: function (json,success,error) { // 添加砼标号
			postAjax('stgid/savestgid',json,success,error)
   		},
        stgidSpquanchaSMGradePriceInfo: function (json,success,error) { //查看砼标号
			postAjax('stgid/spquanchaSMGradePriceInfo',json,success,error)
    	},
        editEditemployeeidphoto: function (json,success,error) { // 上传头像
			postAjax('edit/editemployeeidphoto.do',json,success,error)
    	},
        articleImage: function (userFaceName,success,error) { // 获取头像
			postAjax('articleImage/'+userFaceName+'/a.do',{},success,error)
        },
		//获取版本号
		getVersion: function (success,error) {
			postAjax('versionfile/version.txt',{},success,error)
        },
        adminAdminuseradd : function (json,success,error) { // 添加用户的需要下拉
			postAjax('admin/adminuseradd.action',json,success,error);
    	},
        fileupEditFile : function (json, success, error) { // 取消小票签收
			postAjax('fileup/editFile.do', json, success, error)
    	},


		// 查询材料
		caiLiaoGuoBang: function (json, success, error) {
			postAjax('stockin/spquerydownsrq.do', json, success, error)
        },
        spinsertUpDelVMPersonalWorkClass: function (json, success, error) {
            postAjax('produce/spinsertUpDelVMPersonalWorkClass', json, success, error)
        }

	}
})(window)
